import BigNumber from 'bignumber.js'
import dayjs from 'dayjs'

export function getFormData(obj, enums) {
  const data = {}
  enums.forEach(item => {
    if (item.type === 'input') {
      //
      if (item.amount) {
        data[item.label] = BigNumber(obj[item.field] || 0).toFormat(2, {
          prefix: '',
          decimalSeparator: '.',
          groupSeparator: ',',
          groupSize: 3,
          secondaryGroupSize: 3
        })
      } else if (item.date) {
        data[item.label] = dayjs(obj[item.field]).format(item.format || 'YYYY-MM-DD HH:mm:ss')
      } else if (item.address) {
        // 处理地址
        const objField = obj[item.field]
        if (objField && Array.isArray(objField)) {
          let str = ''
          objField.forEach(f => {
            str = str + `${f.province || ''}${f.city || ''}${f.county || ''}${f.address || ''}\n`
          })
          data[item.label] = str
        } else if (objField && typeof objField === 'object') {
          data[item.label] = `${objField.province || ''}${objField.city || ''}${objField.county || ''}${objField.address || ''}`
        } else {
          data[item.label] = ''
        }
      } else {
        data[item.label] = obj[item.field]
      }
    } else if (item.type === 'select') {
      console.log('data===:', item.label, obj[item.field], item.options)
      const f = item.options.find(f => f.value === obj[item.field])
      data[item.label] = f ? f.label : ''
    }
  })
  console.log('data===:', data)
  return data
}
export function mapFormData(obj, fields) {
  const data = {}
  fields.forEach(item => {
    data[item.vModel] = obj[item.label] || ''
  })
  return data
}
